package com.atom.test.opengl

import android.annotation.SuppressLint
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.MotionEvent
import android.view.View
import android.widget.FrameLayout
import android.widget.ImageView
import com.atom.module.lib.opengles.ext.*
import com.atom.module.logger.Logger
import com.atom.test.opengl.gesture.*
import java.util.*

class FrameLayoutActivity : AppCompatActivity(), GestureListener {

    protected var panRecognizer = PanRecognizer()
    protected var pinchRecognizer = PinchRecognizer()
    protected var rotationRecognizer = RotationRecognizer()
    protected var allRecognizers = Arrays.asList(
        panRecognizer, pinchRecognizer, rotationRecognizer
    )

    lateinit var view: ImageView
    lateinit var frame: FrameLayout

    @SuppressLint("ClickableViewAccessibility")
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_frame_layout)

        frame = findViewById(R.id.contentView)
        frame.setOnTouchListener { v, event ->
            var handled = false
            for (idx in 0 until allRecognizers.size) {
                handled = handled or allRecognizers[idx].onTouchEvent(event)
            }
            handled
        }
        for (entry in allRecognizers) {
            entry.addListener(this)
        }

        view = findViewById(R.id.imageView)
    }

    override fun gestureStateChanged(event: MotionEvent, recognizer: GestureRecognizer) {
        if (recognizer === panRecognizer) {
            val layoutParams = view.layoutParams as FrameLayout.LayoutParams
            layoutParams.leftMargin = layoutParams.marginStart + recognizer.panX().toInt()
            layoutParams.topMargin = layoutParams.topMargin + recognizer.panY().toInt()
            Logger.e(
                """
                    panRecognizer >
                    pan = ${recognizer.panX()}-${recognizer.panY()} 
                    layoutParams = 
                                               ${layoutParams.topMargin}
                     ${layoutParams.leftMargin} --|-- ${layoutParams.rightMargin}
                                               ${layoutParams.bottomMargin}
                """.trimIndent()
            )
            frame.width
            view.requestLayout()
        } else if (recognizer === pinchRecognizer) {
            val scale = recognizer.scale()
            val fl = scale - 1
            val layoutParams = view.layoutParams
            var width = view.width + (view.width * fl).toInt()
            var height = view.height + (view.height * fl).toInt()

            layoutParams.width = width
            layoutParams.height = height
            Logger.e(
                "pinchRecognizer > \n" +
                        "scale = ${scale} \n " +
                        "layoutParams = ${layoutParams.width} ${layoutParams.height} \n "
            )

        } else if (recognizer === rotationRecognizer) {
            var rotation = recognizer.rotation()
            Logger.e(
                "rotationRecognizer > \n" +
                        "rotation = ${rotation} \n "
            )
            view.rotation = rotation
        }
    }


}
